package com.k.kc.util.base64.base64url;

import com.k.kc.util.HashKit;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.lang.*;
import java.lang.Exception;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;

/**
String part1 = base64UrlEncode(header)
String part2 = base64UrlEncode(payload)
String part3 = base64UrlEncode(
 HMACSHA256(
 part1 + "." +
 part2,
 secret)
 )
 String jws = part1 +"."+part2+"."+part3
 https://datatracker.ietf.org/doc/html/rfc4648#section-5
 BASE64URL is an RFC 4648 Base64URL decoder
 * @author k
 */
public class TestBase64Url {
    public static void main(String[] args) {
        try {
            testGenJwt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // same as https://jwt.io/
    // start
    // String base64header = base64urlencode(header);
    // String base64payload = base64urlencode(payload);
    // String content = base64header + "." + base64payload;
    // String signature = HmacSHA256(content,key);
    // String jws = base64header +"."+base64payload + base64urlencode(signature)
    // over
    static void testGenJwt() throws Exception {
        String header = "{\"alg\":\"HS256\"}";
        String claims="{\"sub\":\"Joe\"}";
        header="{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
        claims="{\"sub\":\"12啊3456气味儿童789看0\",\"name\":\"Tom猫个的12\"}";
        String encodedHeader =Base64UrlUtility.encode(header.getBytes("UTF-8"));
        String encodedClaims = Base64UrlUtility.encode(claims.getBytes("UTF-8"));

        String concatenated = encodedHeader + '.' + encodedClaims;
        byte[] keyArr = "12345678901234567890123456789012".getBytes(StandardCharsets.UTF_8);
        byte[] signature = HashKit.HmacSHA256( concatenated.getBytes(Charset.forName("UTF-8")), keyArr );
        String jws = concatenated + '.' + Base64UrlUtility.encode( signature );
        System.out.println("gen jws:"+jws);
        // eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0
        // eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0=
        System.out.println("标准 Base64:");
        System.out.println(Base64.getEncoder().encodeToString(claims.getBytes("UTF-8")));
        System.out.println("Base64 URLSafe:");
        System.out.println(encodedClaims);

        // 生成 jwt 的三个步骤
        String header1 = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
        String body1 = "{\"sub\":\"12啊3456气味儿童789看0\",\"name\":\"Tom猫个的12\"}";
        byte[] signature1 = HashKit.HmacSHA256( concatenated.getBytes(Charset.forName("UTF-8")), "12345678901234567890123456789012".getBytes(StandardCharsets.UTF_8) );

        String headerBase64URLSafeStr = com.k.kc.util.base64.Base64.encodeBase64URLSafeString(header1.getBytes("UTF-8"));
        String bodyBase64URLSafeStr = com.k.kc.util.base64.Base64.encodeBase64URLSafeString(body1.getBytes("UTF-8"));
        String sinatureBase64URLSafeStr = com.k.kc.util.base64.Base64.encodeBase64URLSafeString(signature1);

        String jwt = headerBase64URLSafeStr + "." + bodyBase64URLSafeStr + "." +sinatureBase64URLSafeStr;
        System.out.println("jwt: "+jwt);


    }
}
